package com.kun.companion.core.exception;

import cn.hutool.core.util.StrUtil;

/**
 * 自定义基础运行时异常
 *
 * @author gzc
 * @since 2024/3/9
 */
public class BizException extends RuntimeException {
    /**
     * 错误码
     */
    protected Integer errCode;
    /**
     * 错误信息
     */
    protected String errMsg;

    public BizException() {
        super();
        // 赋值当前线程的堆栈信息，可获取到抛出该异常的具体代码位置
        setStackTrace(Thread.currentThread().getStackTrace());
    }

    public BizException(String errMsg) {
        super(errMsg);
        this.errCode = 4010;
        this.errMsg = errMsg;
        // 赋值当前线程的堆栈信息，可获取到抛出该异常的具体代码位置
        setStackTrace(Thread.currentThread().getStackTrace());
    }

    public BizException(String msg, Throwable cause) {
        super(msg, cause);
        this.errMsg = msg;
        // 赋值当前线程的堆栈信息，可获取到抛出该异常的具体代码位置
        setStackTrace(Thread.currentThread().getStackTrace());
    }

    public BizException(Throwable cause, String msg, Object... params) {
        super(StrUtil.format(msg, params), cause);
        this.errMsg = StrUtil.format(msg, params);
        // 赋值当前线程的堆栈信息，可获取到抛出该异常的具体代码位置
        setStackTrace(Thread.currentThread().getStackTrace());
    }

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }

    @Override
    public String getMessage() {
        return this.errMsg;
    }

    public Integer getErrCode() {
        return errCode;
    }

    public void setErrCode(Integer errCode) {
        this.errCode = errCode;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }
}
